package vip.meeet.jdk.runtime;

import java.util.concurrent.TimeUnit;

public class ShutDown {

    private static volatile boolean done = false;

    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();
        new Thread(() -> {
            while (!done){
                System.out.println("not done");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            for (int i=0;i<10;i++){
                System.out.println("done");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        runtime.addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                done = true;
                try {
                    TimeUnit.SECONDS.sleep(60);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }));
    }
}
